package com.ztao.kafkaLister;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.model.common.apArticle.pojos.ApArticleConfig;
import com.ztao.mapper.ApArticleConfigMapper;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Date 2024/6/1 10:41
 * @Author
 **/
@Component
public class ArticleDownOrUpListener {

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "articleDownOrUp444")
    public void articleDownOrUp(String msg) {
        //msg 就是接收到的信息
        System.out.println("接收到了kafka的消息" + msg);
        Map<String, Object> map = JSON.parseObject(msg, Map.class);

        //修改文章的状态
        QueryWrapper<ApArticleConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApArticleConfig::getArticleId, map.get("articleId"));

        ApArticleConfig config = apArticleConfigMapper.selectOne(queryWrapper);

/*        if((int)map.get("enable")==1){
            config.setIsDown(true);
        }else{
            config.setIsDown(false);
        }*/

          boolean enable = true;

        if ((int) map.get("enable") == 1) {
            enable = false;
        }


        config.setIsDown(enable);

        apArticleConfigMapper.updateById(config);
    }
}